//package com.wxl.rocketmq.test.consumer;
//
//import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
//import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
//import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
//import org.apache.rocketmq.spring.core.RocketMQTemplate;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.support.MessageBuilder;
//import org.springframework.stereotype.Component;
//
//@Component
//@RocketMQTransactionListener(txProducerGroup = "my-transaction-group")
//public class TransactionListenerImpl implements RocketMQLocalTransactionListener {
//
//    @Autowired
//    private RocketMQTemplate rocketMQTemplate;
//
//    @Override
//    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
//        try {
//            // 执行本地事务（如数据库操作）
//            boolean success = doLocalBusiness();
//            return success ? RocketMQLocalTransactionState.COMMIT : RocketMQLocalTransactionState.ROLLBACK;
//        } catch (Exception e) {
//            return RocketMQLocalTransactionState.ROLLBACK;
//        }
//    }
//
//    @Override
//    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
//        // 检查本地事务状态（补偿机制）
//        return checkBusinessStatus() ? RocketMQLocalTransactionState.COMMIT : RocketMQLocalTransactionState.ROLLBACK;
//    }
//
//    // 示例方法：发送事务消息
//    public void sendTransactionMessage(String topic, String message) {
//        rocketMQTemplate.sendMessageInTransaction(
//                "my-transaction-group",
//                topic,
//                MessageBuilder.withPayload(message).build(),
//                null
//        );
//    }
//}
